/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerTypesPnl.java
 *
 * Created on Jul 17, 2011, 11:52:36 AM
 */
package com.viweb.storemanager.ui.customer;

import com.viweb.storemanager.core.ColumnData;
import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.DefaultObjectTableModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Customer;
import com.viweb.storemanager.entity.TypeCustomer;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

/**
 *
 * @author qhvic
 */
public class CustomerTypesPnl extends javax.swing.JPanel {

    private JTable headerTable;
    private ArrayList<TypeCustomer> typeCustomers;
    private CustomerTypesAddEditPnl addEditPanel;

    /** Creates new form CustomerTypesPnl */
    public CustomerTypesPnl() {
        this.typeCustomers = new ArrayList<TypeCustomer>();
        initTypeCustomerList();
        initComponents();
        addEditPanel = new CustomerTypesAddEditPnl(this);
    }

    public CustomerTypesAddEditPnl getCreationPanel() {
        return this.addEditPanel;
    }

    private void initTypeCustomerList() {
        this.typeCustomers.clear();
        this.typeCustomers = Biz.getInstance().selectAllTypeCustomer();
    }

    private void tblListSelectionValueChanged(javax.swing.event.ListSelectionEvent evt) {
        if (!evt.getValueIsAdjusting()) {
            if (this.tblList.getSelectedRow() >= 0) {
                TypeCustomer typeCustomer = this.typeCustomers.get(this.tblList.getSelectedRow());
                this.addEditPanel.assignTypeCustomer(typeCustomer);
                this.addEditPanel.setEditableMode(false);
            }
        }
    }

    public void updateComponentsUI(TypeCustomer typeCustomer) {
        this.initTypeCustomerList();
        int rowSelection = this.typeCustomers.indexOf(typeCustomer);
        this.tblList.getSelectionModel().setSelectionInterval(rowSelection, rowSelection);
        this.headerTable.updateUI();
        this.tblList.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scpList = new javax.swing.JScrollPane();
        tblList = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        scpList.setPreferredSize(new java.awt.Dimension(452, 402));

        tblList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblList.setRowHeight(22);
        ColumnData[] tableColumns = {
            new ColumnData("Mã loại khách hàng", 100, SwingConstants.LEFT, 1),
            new ColumnData("Tên loại khách hàng", 100, SwingConstants.LEFT, 2),
            new ColumnData("Tình trạng", 100, SwingConstants.LEFT, 3),
        };
        DefaultObjectTableModel tableModel = new DefaultObjectTableModel(tblList, tableColumns, typeCustomers, false);

        headerTable = tableModel.getHeaderTable();
        headerTable.createDefaultColumnsFromModel();
        tblList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tblList.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tblListSelectionValueChanged(evt);
            }
        });

        // Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scpList.setRowHeader(viewport);
        scpList.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable
            .getTableHeader());
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListMouseClicked(evt);
            }
        });
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListMouseClicked(evt);
            }
        });
        scpList.setViewportView(tblList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(scpList, gridBagConstraints);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/add.png"))); // NOI18N
        btnAdd.setText("Thêm");
        btnAdd.setToolTipText("Thêm mới nhân viên");
        btnAdd.setMaximumSize(new java.awt.Dimension(80, 26));
        btnAdd.setMinimumSize(new java.awt.Dimension(80, 26));
        btnAdd.setPreferredSize(new java.awt.Dimension(80, 26));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnAdd, gridBagConstraints);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/edit.png"))); // NOI18N
        btnEdit.setText("Sửa");
        btnEdit.setToolTipText("Hiệu chỉnh thông tin nhân viên");
        btnEdit.setMaximumSize(new java.awt.Dimension(80, 26));
        btnEdit.setMinimumSize(new java.awt.Dimension(80, 26));
        btnEdit.setPreferredSize(new java.awt.Dimension(80, 26));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnEdit, gridBagConstraints);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/remove.png"))); // NOI18N
        btnDelete.setText("Xóa");
        btnDelete.setToolTipText("Xóa nhân viên");
        btnDelete.setMaximumSize(new java.awt.Dimension(80, 26));
        btnDelete.setMinimumSize(new java.awt.Dimension(80, 26));
        btnDelete.setPreferredSize(new java.awt.Dimension(80, 26));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnDelete, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tblListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            if (this.tblList.getSelectedRow() != -1) {
                this.addEditPanel.setEditableMode(true);
            } else {
                OptionPane.show(JOptionPane.getFrameForComponent(CustomerTypesPnl.this),
                        "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
            }
        }
    }//GEN-LAST:event_tblListMouseClicked

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        TypeCustomer typeCustomer = new TypeCustomer();
        this.addEditPanel.assignTypeCustomer(typeCustomer);
        this.addEditPanel.setEditableMode(true);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            this.addEditPanel.setEditableMode(true);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(CustomerTypesPnl.this),
                    "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            CursorManager.setWaitCursor(this);

            if (OptionPane.show(JOptionPane.getFrameForComponent(CustomerTypesPnl.this),
                    "Khách hàng và tất cả các thông tin liên quan sẽ bị xoá!\n"
                    + "Có chắc chắn thực hiện thao tác này không?",
                    "Thông báo",
                    OptionPane.MsgTYPE_YESNO) == OptionPane.MsgRESULT_YES) {

                TypeCustomer typeCustomer = this.typeCustomers.get(this.tblList.getSelectedRow());

                if (!Biz.getInstance().deleteCustomer(typeCustomer.getId())) {
                    OptionPane.show(JOptionPane.getFrameForComponent(CustomerTypesPnl.this),
                            Biz.getInstance().getLastError(),
                            "Thông báo");
                } else {
                    int rowSelection = this.typeCustomers.indexOf(typeCustomer);
                    if (rowSelection == this.typeCustomers.size() - 1) {
                        this.typeCustomers.remove(typeCustomer);
                        if (rowSelection != 0) {
                            this.tblList.getSelectionModel().setSelectionInterval(rowSelection - 1,
                                    rowSelection - 1);
                        } else {
                            this.tblList.getSelectionModel().clearSelection();
                        }
                    } else {
                        this.typeCustomers.remove(typeCustomer);
                    }

                    this.headerTable.updateUI();
                    this.tblList.updateUI();
                }

                if (this.tblList.getSelectedRow() >= 0) {
                    TypeCustomer currentTypeCustomer = this.typeCustomers.get(this.tblList.getSelectedRow());
                    this.addEditPanel.assignTypeCustomer(currentTypeCustomer);
                } else {
                    this.addEditPanel.assignTypeCustomer(null);
                }
            }

            CursorManager.setDefaultCursor(this);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(CustomerTypesPnl.this),
                    "Chưa chọn thông tin cần xoá!",
                    "Thông báo");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JScrollPane scpList;
    private javax.swing.JTable tblList;
    // End of variables declaration//GEN-END:variables
}
